package beans;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import mappers.QuizzeMapper;
import model.Quizze;
import dto.QuizzeDTO;

/**
 * Session Bean implementation class QuestionaireBean
 */
@Stateless
public class QuestionaireBean implements QuestionaireBeanRemote {

	
	@PersistenceContext(name ="defaultPersistenceUnit") 
	EntityManager em;

    /**
     * Default constructor. 
     */
    public QuestionaireBean() {
       
    }
    
    
	public String addQuizze(QuizzeDTO dto) {
		String value = "success";
		Quizze dom = (Quizze) new QuizzeMapper().mapDTOtoDOM(dto);
		try {
			em.persist(dom);
		} catch (Exception ex) {
			value = "error";
		}	
		return value;
	}
    
	@Override
	public List<QuizzeDTO> getAllQuizzes() {
		TypedQuery<Quizze> query = em.createNamedQuery("Quizze.findAll", Quizze.class);
		List<QuizzeDTO> result = new ArrayList<QuizzeDTO>();
		QuizzeMapper mapper = new QuizzeMapper();
		for (Quizze dom : query.getResultList()) {
			result.add(mapper.mapDOMtoDTO(dom));
		}
		return result;
	}

}
